<template>
  <div class="">
    <!-- app-container -->
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="" prop="">
          <el-input v-model="queryParams.query" placeholder="请输入商品名称、商品id" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="库存：" prop="">
          <el-select v-model="queryParams.stockStatus" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="dict in stockStatusLs" :key="dict.id" :label="dict.value" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="销售状态：" prop="">
          <el-select v-model="queryParams.salesStatus" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="(item,index) in salesStatusLs" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用状态：" prop="">
          <el-select v-model="queryParams.useStatus" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="(item,index) in useStatusLs" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" size="small" @click="updateList" :loading="updateListLoading">更新商品列表</el-button>
        <el-button type="primary" size="small" @click="updateProduct" :disabled="productIds.length>0?false:true" :loading="updateDetailLoading">更新商品信息</el-button>
      </div>
      <el-table ref="tableRef" v-loading="loading" :data="tabList" stripe border row-key="id"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" :reserve-selection="true" width="55">
        </el-table-column>
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="商品id" align="center" prop="productId" :show-overflow-tooltip="true" />
        <el-table-column label="商品类型" align="center" prop="productType" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="productTypeLs" :value="scope.row.productType" />
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="面值" align="center" prop="faceValue" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="单价" align="center" prop="purchasePrice" :show-overflow-tooltip="true" />
        <el-table-column label="库存" align="center" prop="stockStatus" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="stockStatusLs" :value="scope.row.stockStatus" />
          </template>
        </el-table-column>
        <el-table-column label="商品详情" align="center" prop="details" :show-overflow-tooltip="true" />
        <el-table-column label="销售状态" align="center" prop="salesStatus" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="salesStatusLs" :value="scope.row.salesStatus" />
          </template>
        </el-table-column>
        <el-table-column label="使用状态" align="center" prop="useStatus" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="useStatusLs" :value="scope.row.useStatus" />
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
  import {
    thirdCommodityList,
    updateThirdCommodityList,
    updateThirdCommodityDetail
  } from "@/api/consumeMiningManagement/thirdPartyGoodsLs";
  export default {
    // dicts: ['equity_card_issue_type', 'equity_card_sell_status'],
    name: "",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: false,
        updateListLoading: false,
        updateDetailLoading: false,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        productIds: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 表格数据
        tabList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productType: '', //商品类型 shenye_third_commodity_product_type
          query: '', //商品名称、商品id
          salesStatus: '', //销售状态 shenye_third_commodity_sales_status	
          stockStatus: '', //库存状态 shenye_third_commodity_stock_status
          useStatus: '', //使用状态 shenye_third_commodity_use_status
        },
        productTypeLs: [], //商品类型数组
        salesStatusLs: [], //销售状态数组
        stockStatusLs: [], //库存状态数组
        useStatusLs: [], //使用状态数组
      };
    },
    created() {
      this.getProductTypeLs();
      this.getSalesStatusLs();
      this.getStockStatusLs();
      this.getUseStatusLs();
      this.getList();
    },
    methods: {
      //获取商品类型
      getProductTypeLs() {
        var dictType = "shenye_third_commodity_product_type";
        this.$getSelectList(dictType)
          .then((res) => {
            var arr = [{
              id: "",
              value: "全部",
            }, ];
            this.productTypeLs = arr.concat(res.result[0].dictList);
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取销售状态字典
      getSalesStatusLs() {
        var dictType = "shenye_third_commodity_sales_status";
        this.$getSelectList(dictType)
          .then((res) => {
            var arr = [{
              id: "",
              value: "全部",
            }, ];
            this.salesStatusLs = arr.concat(res.result[0].dictList);
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取库存状态字典
      getStockStatusLs() {
        var dictType = "shenye_third_commodity_stock_status";
        this.$getSelectList(dictType)
          .then((res) => {
            var arr = [{
              id: "",
              value: "全部",
            }, ];
            this.stockStatusLs = arr.concat(res.result[0].dictList);
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取使用状态字典
      getUseStatusLs() {
        var dictType = "shenye_third_commodity_use_status";
        this.$getSelectList(dictType)
          .then((res) => {
            var arr = [{
              id: "",
              value: "全部",
            }, ];
            this.useStatusLs = arr.concat(res.result[0].dictList);
          })
          .finally(() => {
            this.loading = false;
          });
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        thirdCommodityList(this.queryParams).then(response => {
          this.tabList = response.result.records;
          this.total = response.result.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.date = []
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          productType: '', //商品类型 shenye_third_commodity_product_type
          query: '', //商品名称、商品id
          salesStatus: '', //销售状态 shenye_third_commodity_sales_status	
          stockStatus: '', //库存状态 shenye_third_commodity_stock_status
          useStatus: '', //使用状态 shenye_third_commodity_use_status
        }
        // this.resetForm("form");
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.reset();
      },
      /** 更新商品列表按钮操作 */
      updateList() {
        this.updateListLoading = true
        updateThirdCommodityList().then(res => {
          this.$message.success('更新成功');
          this.updateListLoading = false
          this.reset()
        }).catch(()=>{
          this.updateListLoading = false
        })
      },
      /** 更新商品信息 */
      updateProduct(){
        this.updateDetailLoading = true
        updateThirdCommodityDetail(this.productIds).then(res => {
          this.$message.success('更新成功');
          this.getList();
          this.updateDetailLoading = false
          this.$refs.tableRef.clearSelection()
        }).catch(()=>{
          this.updateDetailLoading = false
        })
      },
      /** 多选框选中数据 */
      handleSelectionChange(selection) {
        this.productIds = selection.map(item => item.productId)
        console.log(this.productIds)
      },
    }
  };

</script>
<style scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    padding-bottom: 20px;
  }

</style>
